package com.dyb.recuitment.controller;


import com.dyb.recuitment.common.Result;
import com.dyb.recuitment.entity.Setting;
import com.dyb.recuitment.entity.UserLog;
import com.dyb.recuitment.service.IUserLogService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * <p>
 * 用户日志表 前端控制器
 * </p>
 *
 * @author 邓宇斌
 * @since 2022-02-21
 */
@RestController
@Api(tags = "用户日志管理(UserLog)")
@RequestMapping("/log")
public class UserLogController {

    @Autowired
    private IUserLogService userLogService;

    @ApiOperation(value = "添加日志", notes = "通过用户ID、操作类型、key、value添加日志 " +
            "{\"user_id\":1,\"operate\":\"\",\"key\":\"\",\"value\":\"\"}")
    @ApiImplicitParam(name = "userLog", value = "UserLog类")
    @PostMapping("/add")
    public Object add(@RequestBody UserLog userLog) {
        Object data = null;
        try {
            data = userLogService.add(userLog);
            if (data == null || (int) data == 0) {
                return Result.failure(data, "添加日志失败!");
            }
            return Result.success(data, "添加日志成功!");
        } catch (Exception e) {
            System.out.println(e.getMessage());
        }
        return Result.failure(data);
    }

    @ApiOperation(value = "查询日志", notes = "按用户ID查询日志 {\"userId\":1}")
    @ApiImplicitParam(name = "userLog", value = "UserLog类")
    @PostMapping("/selectByUserId")
    public Object selectByUserId(@RequestBody UserLog userLog) {
        Object data = null;
        try {
            data = userLogService.selectByUserLog(userLog);
            userLogService.add(new UserLog(userLog.getUserId(), "查询指定用户日志"));
            if (data == null || ((List) data).size() <= 0) {
                return Result.failure(data, "查询失败");
            }
            return Result.success(data, "查询成功!");
        } catch (Exception e) {
            System.out.println(e.getMessage());
        }
        return Result.failure(data);
    }

    @ApiOperation(value = "获取所有用户日志", notes = "获取所有用户日志")
    @GetMapping("/selectAllUserLog")
    public Object selectAllUserLog() {
        Object data = null;
        try {
            data = userLogService.selectAllUserLog();
            userLogService.add(new UserLog(0, "获取所有用户日志"));
            if (data == null || ((List) data).size() <= 0) {
                return Result.warning(data, "用户日志表为空!");
            }
            return Result.success(data);
        } catch (Exception e) {
            System.out.println(e.getMessage());
        }
        return Result.failure(data);
    }

}
